/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.edu.nju.software.greencreditsystem.action;

import cn.edu.nju.software.greencreditsystem.domain.EnvironmentExcel;
import cn.edu.nju.software.greencreditsystem.domain.FinancialExcel;
import cn.edu.nju.software.greencreditsystem.factory.ServiceFactory;
import cn.edu.nju.software.greencreditsystem.service.EnvironmentExcelService;
import cn.edu.nju.software.greencreditsystem.service.FinancialExcelService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.sql.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletContext;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.util.ServletContextAware;

/**
 * 作者：韩天
 * 创建时间：2011-8-13
 * 类描述：企业数据提交模块的action封装类
 */
public class EnterpriseUploadAction extends ActionSupport implements ServletContextAware {

    private File upload;// 实际上传文件
    private String uploadContentType; // 文件的内容类型
    private String uploadFileName; // 上传文件名
    private String fileCaption;// 上传文件时的备注
    private ServletContext context;
    
    private int enterpriseId;//企业的id号
    private List<FinancialExcel> fexcelList;//  企业财务报表list
    private List<EnvironmentExcel> eexcelList;//   企业环境报表
    private int fexcelId;// 企业财务报表id
    private int eexcelId;// 企业环境报表id
    private String fileName;//  下载文件的名字
    private Date today;// 当前的日期


    @Override
    /**
     * 方法描述：默认的执行方法，实现无条件跳转
     * param：
     * return：SUCCESS
     */
    public String execute() {
        return SUCCESS;
    }

    /**
     * 方法描述：企业上传财务报表
     * param：
     * return：SUCCESS
     */
    public String upload_FinancialExcel() throws Exception {
        try {
            String targetDirectory = context.getRealPath("/upload");
            String targetFileName = uploadFileName;

            File target = new File(targetDirectory, targetFileName);
            FileUtils.copyFile(upload, target);
            //将文件转成byte数组
            FileInputStream fis = new FileInputStream(target);
            byte[] file = new byte[fis.available()];
            fis.read(file);

            FinancialExcelService fes = ServiceFactory.getFinancialExcelServiceInstance();
            fes.setExcelContent(this.fexcelId, file);
            //删除临时的target文件
            target.delete();
        } catch (Exception e) {
            e.printStackTrace();
            return INPUT;
        }
        return SUCCESS;

    }

    /**
     * 方法描述：企业上传环境报表
     * param：
     * return：SUCCESS
     */
    public String upload_EnvironmentExcel() throws Exception {
        try {
            String targetDirectory = context.getRealPath("/upload");
            String targetFileName = uploadFileName;

            File target = new File(targetDirectory, targetFileName);
            FileUtils.copyFile(upload, target);

            //将文件转成byte数组
            FileInputStream fis = new FileInputStream(target);
            byte[] file = new byte[fis.available()];
            fis.read(file);

            EnvironmentExcelService ees = ServiceFactory.getEnvironmentExcelServiceInstance();
            ees.setExcelContent(eexcelId, file);
            //删除临时的target文件
            target.delete();
        } catch (Exception e) {
            e.printStackTrace();
            return INPUT;
        }
        return SUCCESS;

    }

    /**
     * 方法描述：企业查看报表上传情况
     * param：
     * return：SUCCESS
     */
    public String lookup_Upload() {        
        //取得请求相关的ActionContext实例  
        ActionContext actionContext = ActionContext.getContext();
        Map session = actionContext.getSession();
        enterpriseId = Integer.valueOf((String) session.get("userId"));
        
        //取得财务报表列表
        FinancialExcelService fExcelService = ServiceFactory.getFinancialExcelServiceInstance();
        this.fexcelList = fExcelService.getVisibleExcelList(enterpriseId);
        //取得环境报表列表
        EnvironmentExcelService eExcelService = ServiceFactory.getEnvironmentExcelServiceInstance();
        this.eexcelList = eExcelService.getVisibleExcelList(enterpriseId);
        
        //判断财务报表是否已过期限
        for(int i=0; i<fexcelList.size(); i++){
            if(getToday().before(fexcelList.get(i).getDeadline())){
                fexcelList.get(i).setExpired(false);
            }else{
                fexcelList.get(i).setExpired(true);
            }
        }
        //判断环境报表是否已过期限
        for(int i=0; i<eexcelList.size(); i++){
            if(getToday().before(eexcelList.get(i).getDeadline())){
                eexcelList.get(i).setExpired(false);
            }else{
                eexcelList.get(i).setExpired(true);
            }
        }
        return SUCCESS;
    }

    /**
     * 方法描述：企业下载财务报表模板
     * param：
     * return：SUCCESS
     */
    public String download_Financial() {
        this.fileName = "企业财务报表模板.xls";
        return SUCCESS;
    }

    /**
     * 方法描述：企业下载环境报表模板
     * param：
     * return：SUCCESS
     */
    public String download_Environment() {
        this.fileName = "企业环境报表模板.xls";
        return SUCCESS;
    }
    
    
    
    
    

    public InputStream getInputStream() {
        return ServletActionContext.getServletContext().getResourceAsStream("/" + fileName);
    }

    public ServletContext getContext() {
        return context;
    }

    public void setContext(ServletContext context) {
        this.context = context;
    }

    public String getFileCaption() {
        return fileCaption;
    }

    public void setFileCaption(String fileCaption) {
        this.fileCaption = fileCaption;
    }

    public File getUpload() {
        return upload;
    }

    public void setUpload(File upload) {
        this.upload = upload;
    }

    public String getUploadContentType() {
        return uploadContentType;
    }

    public void setUploadContentType(String uploadContentType) {
        this.uploadContentType = uploadContentType;
    }

    public String getUploadFileName() {
        return uploadFileName;
    }

    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }

    public int getEnterpriseId() {
        return enterpriseId;
    }

    public void setEnterpriseId(int enterpriseId) {
        this.enterpriseId = enterpriseId;
    }

    public List<EnvironmentExcel> getEexcelList() {
        return eexcelList;
    }

    public void setEexcelList(List<EnvironmentExcel> eexcelList) {
        this.eexcelList = eexcelList;
    }

    public List<FinancialExcel> getFexcelList() {
        return fexcelList;
    }

    public void setFexcelList(List<FinancialExcel> fexcelList) {
        this.fexcelList = fexcelList;
    }

    @Override
    public void setServletContext(ServletContext context) {
        this.context = context;
    }

    public int getEexcelId() {
        return eexcelId;
    }

    public void setEexcelId(int eexcelId) {
        this.eexcelId = eexcelId;
    }

    public int getFexcelId() {
        return fexcelId;
    }

    public void setFexcelId(int fexcelId) {
        this.fexcelId = fexcelId;
    }

    public String getFileName() {
        try {
            fileName = new String(fileName.getBytes("GBK"), "ISO8859-1");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public Date getToday() {
        return new Date(System.currentTimeMillis());
    }

    public void setToday(Date today) {
        this.today = today;
    }
}
